/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tesam.gedeon.dao.impl;

import com.tesam.gedeon.dao.NetworkDao;
import com.tesam.gedeon.entity.Network;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Sistema
 */
@Repository
public class NetworkDaoImpl extends SimpleJdbcDaoSupport implements NetworkDao {

    @Autowired
    public NetworkDaoImpl(DataSource dataSource) {
        setDataSource(dataSource);
    }

    @Override
    public List<Network> getListAll() {
        List<Network> result = new ArrayList();
        
        try{
            result = getJdbcTemplate().query("select id , createdby , status , viewedtime , code , description from network where deleted = 0 ",
                  new BeanPropertyRowMapper<Network>(Network.class) ) ;
            System.out.println("getListAll : "+result.size());
        }catch(Exception e){
        System.out.println("getListAll : "+e.getMessage());
        }
        
        return result ;
    }

    @Override
    public List<Network> getAllByLimit(String dataToSort ,String directionOfOrder  , int startLimit, int endLimit) {
        
        List<Network> result = new ArrayList();
        
        try{
             String sql = " select id , createdBy , code , description from network  where deleted = 0 "
                    + "order by "+dataToSort+" "+directionOfOrder+" limit "+endLimit+" offset "+startLimit ;
    
            result = getJdbcTemplate().query(sql, new BeanPropertyRowMapper<Network>(Network.class));
        }catch(Exception e){
        
        }
        
        return result ;
    }

    @Override
    public int getCountAll() {
        return getJdbcTemplate().queryForInt("select count(*) as total from network where deleted = 0");
    }

    @Override
    public List<Network> getSearchNetworkByLimit(String dataToSort, String directionOfOrder, int startLimit, int endLimit, Network network) {
        List<Network> result = new ArrayList();
        
        try{
             String sql = " select * from network  where deleted = 0 and description like '%"+network.getDescription()+"%' "
                    + "order by "+dataToSort+" "+directionOfOrder+" limit "+endLimit+" offset "+startLimit ;
              
             
            result = getJdbcTemplate().query(sql, new BeanPropertyRowMapper<Network>(Network.class));
        }catch(Exception e){
        
        }
        
        return result ;
    }
    
    @Override
    public int getTotalBySearch(Network network){
         return getJdbcTemplate().queryForInt("select count(*) as total from network where deleted = 0 and  description = '"+network.getDescription()+"'");
    }
    
    @Override
    public List<Network> getNetworkById(Network network){
        List<Network> result = new ArrayList();
            try{
                
                getJdbcTemplate().update("update network set viewedtime = NOW() where id = ? ", network.getId());
                
                result = getJdbcTemplate().query("select id , createdby , datecreated ,"
                        + " code , description from network where deleted = 0 and id = ?", 
                        new BeanPropertyRowMapper<Network>(Network.class) , network.getId());
            }catch(Exception e){
            System.out.println("getNetworkById : "+e.getMessage());
            }
        return result;
     }
    
    @Override
    public List<Network> updateNetwork(Network network){
        List<Network> result = new ArrayList();
        
        try{
            String sql = "update network set description = ? , datemodified = NOW() , modifiedby = ? where id = ?" ;
            
            getJdbcTemplate().update(sql , network.getDescription() , network.getModifiedBy() , network.getId());
            
            result = this.getNetworkById(network);
            
        }catch(Exception e){
        
        }
        
        return result ;
    }
    
    @Override
    public void deleteNetwork(Network network){
        try{
            getJdbcTemplate().update("update network set deleted = 1 where id = ?" , network.getId());
        }catch(Exception e){
        
        }
    
    }
    
    @Override
    public int saveNetwork(Network network){
        int id = 0;
        try{ 
            
            getJdbcTemplate().update(" insert into network (createdby , datecreated , status ,"
                    + " deleted , presence , ownerid  , code , description) "
                    + "values(? , NOW() , 1 , "
                    + "0 , 0 , 1 , ? , ? )" , network.getCreatedBy() , network.getCode() , network.getDescription());
            
              id = getSimpleJdbcTemplate().queryForInt("SELECT last_value FROM entity_id_seq");
             
           
            
        }catch(Exception e){
        
        }
        
        return id ;
    }
    
}
